package com.example.administrator.myapplication.form;

import android.util.Log;

import com.example.administrator.myapplication.data.Task;
import com.example.administrator.myapplication.data.source.TaskDataSource;
import com.example.administrator.myapplication.data.source.TaskRepository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2017/11/27.
 */

public class FormPresenter implements FormContract.Presenter{

    private static final String TAG = FormPresenter.class.getCanonicalName();
    private TaskRepository mTaskRepository;
    private FormContract.View mFormView;

    public FormPresenter(TaskRepository taskRepository,FormContract.View formView){
        this.mTaskRepository = taskRepository;
        this.mFormView = formView;
//        formView.setPresenter(this);
    }





    @Override
    public void start() {
        loadTasks();
    }

    @Override
    public void startSelected(String content) {
        getSelectedTask(content);
    }

    @Override
    public void loadTasks() {
        mTaskRepository.getTasks(new TaskDataSource.LoadTasksCallback() {
            @Override
            public void onTasksLoaded(List<Task> tasks) {
                mFormView.showTasks(tasks);
            }

            @Override
            public void onDataNotAvaliable() {
                Log.i(TAG, "onDataNotAvaliable: ");
            }
        });
    }

    @Override
    public void loadTask() {

    }

    @Override
    public String formatDate(long timeMillions) {
        Date date = new Date(timeMillions);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINESE);
        return sdf.format(date);
    }

    @Override
    public void getSelectedTask(String content) {
        mTaskRepository.getSearchedTasks(content, new TaskDataSource.LoadSerachedTasksCallback() {
            @Override
            public void onTasksLoaded(List<Task> tasks) {
                mFormView.showSelectedTask(tasks);
            }

            @Override
            public void onDataNotAvaliable() {
                Log.i(TAG, "onDataNotAvaliable: getSelectedTask");
            }
        });
    }
}
